package com.white.loggerserver.utils;

import android.app.Activity;
import android.content.Context;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import androidx.annotation.ColorRes;
import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;
import androidx.core.content.ContextCompat;

/**
 * Created by ytf on 2018/3/23 023.
 * Description:
 */

public class UiHelper
{
    public static void backgroundAlpha(Activity ac, float bgAlpha)
    {
        WindowManager.LayoutParams lp = ac.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        ac.getWindow().setAttributes(lp);
    }

    public static int screenHeight(Context ctx)
    {
        return ctx.getResources().getDisplayMetrics().heightPixels;
    }

    public static int screenHeightDp(Context ctx)
    {
        DisplayMetrics dm = ctx.getResources().getDisplayMetrics();
        int wid = dm.heightPixels;
        return (int) (wid * 1.0f / dm.density + .5f);
    }

    public static int screenWidth(Context ctx)
    {
        return ctx.getResources().getDisplayMetrics().widthPixels;
    }

    public static int screenWidthDp(Context ctx)
    {
        DisplayMetrics dm = ctx.getResources().getDisplayMetrics();
        int wid = dm.widthPixels;
        return (int) (wid * 1.0f / dm.density + .5f);
    }

    public static void hideKeyboard(Activity activity) {
        InputMethodManager im = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if(im != null)
        {
            View focus = activity.getCurrentFocus();
            if(focus != null)
            {
                im.hideSoftInputFromWindow(focus.getApplicationWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    }

    /**
     * 获取屏幕的宽度
     */
    public static int windowWidth(Activity activity)
    {
        DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        return dm.widthPixels;
    }

    /***
     *
     * @return 获得状态栏高度
     */
    public static int getStatusHight(Context ctx)
    {
        int identifyId = ctx.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (identifyId > 0)
        {
            return ctx.getResources().getDimensionPixelSize(identifyId);
        }
        return 0;
    }

    public static int dpToPx(Context context, float dp)
    {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5);
    }

    public static int pxToDp(Context context, float px)
    {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (px / scale + 0.5);
    }

    public static void setColorfulTxt(TextView tv, String content, int start, int end, int targetColor, final View.OnClickListener... listeners)
    {
        SpannableString ss = new SpannableString(content);
        ForegroundColorSpan fs = new ForegroundColorSpan(targetColor);
        if(listeners != null && listeners.length > 0)
        {
            ClickableSpan click = new ClickableSpan()
            {
                @Override
                public void onClick(@NonNull View widget)
                {
                    listeners[0].onClick(widget);
                }

                @Override
                public void updateDrawState(TextPaint ds) {
                    ds.setColor(ds.linkColor);
                    ds.setUnderlineText(false);    //去除超链接的下划线
                }
            };
            ss.setSpan(click, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            tv.setMovementMethod(LinkMovementMethod.getInstance());
        }
        ss.setSpan(fs, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv.setText(ss);
    }

    /**
     *
     * @param tv
     * @param content
     * @param positions [1,2, 4,5] 两个分组
     * @param targetColors [red, blue]
     */
    public static void setColorfulTxt(TextView tv,String content,int[] positions, int[] targetColors)
    {
        SpannableString ss = new SpannableString(content);

        for (int i = 0; i < positions.length / 2; i++)
        {
            ForegroundColorSpan fs = new ForegroundColorSpan(targetColors[i]);
            ss.setSpan(fs, positions[i * 2], positions[i * 2 + 1], Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        tv.setText(ss);
    }

    public static void setStyleTxt(TextView tv, String content, int start, int end, @StyleRes int style)
    {
        SpannableString ss = new SpannableString(content);
        StyleSpan fs = new StyleSpan(style);
        ss.setSpan(fs, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv.setText(ss);
    }

    public static int getColor(Context ctx, @ColorRes int colorId)
    {
        return ContextCompat.getColor(ctx, colorId);
    }


    public static int gradientColor(int alpha, int color)
    {
        return ((alpha & 0xff) << 24) | (color & 0x00ffffff);
    }
}
